package br.com.viamais.dao;

import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import br.com.viamais.enumerator.Permission;
import br.com.viamais.infra.dao.BaseDAO;
import br.com.viamais.model.Role;

/**
 * <B>Project: viamais-web</B><BR>
 * 
 * DAO that represents database operations of the Role entity.
 * 
 * <DL>
 * <DT><B>Created:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * <DL>
 * <DT><B>Last Modification:</B>
 * <DD>Oct 3, 2011</DD></DT>
 * </DL>
 * 
 * @author Caio Carleto - CarSer - Oct 3, 2011
 * @version 1.0
 */
//TODO implement log.
public class RoleDAO extends BaseDAO<Role>{

	/**
	 * Serial Version ID.
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected Class<Role> getClazz() {
		return Role.class;
	}
	
	/**
	 * Get by permission.
	 * 
	 * @param Permission: permission.
	 * 
	 * @return Role: role.
	 * 
	 * @throws Throwable: connection issues.
	 */
	public Role getByPermission(Permission permission) throws Throwable {
		Role role = null;
		if(permission != null) {
			this.beginTransaction();
			Criteria criteria = this.getSession().createCriteria(getClazz());
			criteria.add(Restrictions.eq("permission", permission));
			role = (Role) criteria.uniqueResult();
			this.commit();
		}
		return role;
	}
}
